package com.change.copy.utils;

import com.change.copy.common.SexEnum;
import com.change.copy.common.VisualAngleEnum;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Slf4j
public abstract class AiUtil {

    /**
     * 获取 ai 发送的信息
     */
    public abstract String getContent(String content);

    /**
     * 重构书名
     */
    public List<String> changeBookTitle() {
        String changeBookTitleSwitch = SpringContextUtil.getValue("aiCommon.changeBookTitleSwitch");
        String changeBookTitleWords = SpringContextUtil.getValue("aiCommon.changeBookTitleWords");
        String originalBookName = SpringContextUtil.getValue("aiCommon.originalBookName");

        List<String> list = Lists.newArrayList();
        if (Boolean.parseBoolean(changeBookTitleSwitch)) {
            String content = changeBookTitleWords.replace("?", originalBookName);
            String sparkContent = getContent(content);
            for (String bookName : sparkContent.split("\n")) {
                list.add(bookName);
                log.info(bookName);
            }
        }
        return list;
    }

    /**
     * 转换人称
     * revisePerson 修改人称话术
     * text 文案文本
     * protagonistName 主角名
     * sex 性别 中文 男性/女性
     */
    public String convertPerson(String text) {
        try {
            String revisePersonSwitch = SpringContextUtil.getValue("aiCommon.revisePersonSwitch");
            // 如果不启用重构人称则直接返回
            if (!Boolean.parseBoolean(revisePersonSwitch)) {
                return text;
            }
            String revisePerson = SpringContextUtil.getValue("aiCommon.revisePerson");
            String protagonistName = SpringContextUtil.getValue("name");

            String remarks = VisualAngleEnum.getRemarks();
            String sex = SexEnum.getRemarks();
            // 处理人称话术
            revisePerson = revisePerson.replace("!(?)", remarks)
                    .replace("@(?)", protagonistName)
                    .replace("#(?)", sex);

            // 以 2000 字为分割
            int groupSize = 2000;
            String replace = text.replace("\n", "")
                    .replace(" ", "")
                    .replace("　", "")
                    .replace("\t", "")
                    .replace(" ", "");
            List<String> stringList = IntStream.iterate(0, n -> n + groupSize)
                    .limit((long) Math.ceil((double) replace.length() / groupSize))
                    .mapToObj(i -> replace.substring(i, Math.min(i + groupSize, replace.length())))
                    .collect(Collectors.toList());
            StringBuilder stringBuilder = new StringBuilder();
            for (String str : stringList) {
                String content = getContent(revisePerson + str);
                stringBuilder.append(content);
            }
            return stringBuilder.toString();
        } catch (Exception e) {
            log.error("convertPerson 报错 如果是超时 请修改 groupSize (分割文案)", e);
            throw e;
        }
    }


}
